<template>
    <el-row class="row-bg" justify="start">
        <a href="#/main/cms/category/create">
            <el-button type="primary">
                创建
            </el-button>
        </a>
    </el-row>

    <div style="height:15px"></div>
    <el-table ref="singleTable" :data="categoryList" highlight-current-row @current-change="handleCurrentChange"
        style="width: 100%">

        <el-table-column property="id" label="ID" width="80">
        </el-table-column>
        <el-table-column property="name" label="名称" width="">
        </el-table-column>
        <el-table-column property="path_name" label="路径" width="">

        </el-table-column>
        <el-table-column property="is_recommend" label="推荐" width="">
            <template #default="scope">
                <span v-if="scope.row.is_recommend == 1">是</span>
                <span v-if="scope.row.is_recommend == 0">否</span>
            </template>
        </el-table-column>
        <el-table-column property="sort_order" label="排序" width="">
        </el-table-column>

        <el-table-column align="center" label="操作" width="">
            <template #default="scope">
                <a class="el-button el-button--primary el-button--small" :href="'#/main/cms/category/create/' + scope.row.id">
                    添加子分类
                </a>
                <a class="el-button el-button--primary el-button--small" :href="'#/main/cms/category/edit/' + scope.row.id">
                    编辑
                </a>
                <a class="el-button el-button--primary el-button--small" @click="deleteCategory(scope.row.id)">
                    删除
                </a>
            </template>
        </el-table-column>

    </el-table>

    <div class=" table-below ">

        <el-pagination @current-change="changePage" :page-size="per_page" layout="prev, pager, next" :total="total" />
    </div>
</template>

<script lang="ts" setup>

import axios from 'axios';
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const search: any = ref<object>({
    name: ''
})
const router: any = useRouter();
const categoryList: any = ref<object>()
const per_page: any = ref<number>(10);
const total: any = ref<number>();
const changePage: any = ref<number>(1);


const handleCurrentChange = () => {

}
const getList = () => {

    const url = window.VITE_URL + '/admin.cms.category/index';

    // console.log(url,data)
    axios.get(url).then(res => {

        categoryList.value = res.data;

    });
}
getList();

// 编辑分类





const deleteCategory = (id: number) => {

    let url: string = window.VITE_URL + '/admin.cms.category/delete';
    url += '?id=' + id;
    axios.get(url).then(function (res) {

        ElMessage({
            'message': res.data.status == 0 ? '修改成功' : res.data.message,
            duration: 1000,
            type: res.data.status == 0 ? 'success' : 'error',
            onClose: function () {
                // 'on-close':function(){
                // router.push({path:'/main/cms/category'});
                getList();
            }
        })

    });

}



</script>